
window.UIHelper = cc.Class({
    extends: cc.Component,

    properties: {
    },
        statics: {
            mIsHideLoadingTips: false,
            createPrefab: function (prefab) {
                let prefabNode = null;
                if (cc.supportJit) {
                    cc.supportJit = false;
                    prefabNode = cc.instantiate(prefab);
                    cc.supportJit = true;
                }
                else {
                    // 如果是特殊的不支持 JIT 的平台，则不需要优化，也不能强制设置 cc.supportJit 为 true
                    prefabNode = cc.instantiate(prefab);
                }
                return prefabNode;
            },

            loadRes: function (url, type, callback) {
                if (!url) {
                    return;
                }
                cc.loader.loadRes(url, type, function (err, res) {
                    if (err) {
                        cc.log('---------loadRes err: ' + err);
                        return;
                    }
                    if (callback) {
                        callback(res);
                    }
                });
            },

            convertToTargetNodeSpace: function (srcNode, destNode) {
                let worldPos = srcNode.convertToWorldSpaceAR(cc.p(0, 0));
                let nodePost = destNode.parent.convertToNodeSpaceAR(worldPos);
                return nodePost;
            },

            isChinese: function () {
                var currentLanguageType = cc.sys.language;
                if (currentLanguageType == cc.sys.LANGUAGE_CHINESE) {
                    return true;
                }
                return false;
            },

            //控制UI最大宽度(通过缩放UI实现)
            scaleUIMaxWidthByWin: function (uiNode, offsetWidth) {
                if (typeof (offsetWidth) == 'undefined') {
                    offsetWidth = 50;
                }
                let winWidth = cc.director.getWinSize().width;
                let maxWidth = winWidth - offsetWidth;
                if (uiNode.width > maxWidth) {
                    let scale = maxWidth / uiNode.width;
                    uiNode.scale = scale;
                }
            },

            //处理多过长的文字内容，过长的内容用省略号代替
            longMsgUseEllipsis: function (msg, maxWidth, fontSize) {
                // cc.log('-------------------longMsgUseEllipsis_msg: ' + msg)

                let msgNode = new cc.Node();
                msgNode.x = 10000;
                let msgLabel = msgNode.addComponent(cc.Label);
                msgLabel.fontSize = fontSize;
                cc.director.getScene().addChild(msgNode);
                msgLabel.string = msg;
                let curWidth = msgNode.width;
                while (curWidth > maxWidth) {
                    let offsetPercent = (curWidth - maxWidth) / curWidth;
                    let offsetLength = Math.ceil(msg.length * offsetPercent);
                    msg = msg.substring(0, msg.length - offsetLength);
                    msgLabel.string = msg;
                    curWidth = msgNode.width;
                };
                msgNode.destroy();
                // return msg + '..';
                return msg;
            },

            loadNetImg: function (container, addres) {
                if (container == null || container == 'null') {
                    return;
                }
                if (addres == null || addres == 'null') {
                    return;
                }
                if(addres === ""){
                    return;
                }
                if (addres.indexOf('http') == 0)
                {
                    cc.loader.load({ url: addres, type: 'png' }, function (err, spFrame) {
                        container.spriteFrame = new cc.SpriteFrame(spFrame)
                    });
                }
                else
                {
                    if(SystemInfo.getPlatform() == 'qqPlay'){
                        this.loadQQNet(container,addres);
                    }

                }
            },
            loadQQNet: function (container, addres) {
                BK.MQQ.Account.getHeadEx(addres, function (openId, imgPath) {
                    let image = new Image();
                    image.onload = function () {
                        var tex = new cc.Texture2D();
                        tex.initWithElement(image);
                        tex.handleLoadedTexture();
                        container.spriteFrame = new cc.SpriteFrame(tex);
                    };
                    image.src = imgPath;
                });
            },
            getTransScenePersistNodeJs: function () {
                let transPersistNode = cc.director.getScene().getChildByName('TransitionSceneNode');
                if (transPersistNode) {
                    return transPersistNode.getComponent('TransitionSceneNode');
                }
                return null;
            },

            getTransScenePersistNode: function(){
                let transPersistNode = cc.director.getScene().getChildByName('TransitionSceneNode');
                return transPersistNode;
            },

            showLoaing: function(){
                // let parent = cc.director.getScene();
                // if (UIHelper.isHaveChildByName(parent, "loadingTips")) {
                //     return;
                // }
                // var self = this;
                // this.addPrefabUI('Prefab/LoadingTips', parent, 100, function (loadingTips) {
                //     loadingTips.name = 'loadingTips';
                //     if(self.mIsHideLoadingTips){
                //         self.mIsHideLoadingTips = false;
                //         loadingTips.destroy();
                //     }
                // });
            },
        
            hideLoaing: function(){
                // let parent = cc.director.getScene();
                // let childNode = parent.getChildByName("loadingTips");
                // this.mIsHideLoadingTips = true;
                // if(childNode){
                //     this.mIsHideLoadingTips = false;
                //     childNode.destroy();
                // }
            },

            showTips: function(tips,content,defineLabel,remainTips){
                // if(!this.getTransScenePersistNodeJs()){
                //     return;
                // }
                // this.getTransScenePersistNodeJs().showTips(tips,content,defineLabel,remainTips);
            },

            isHaveChildByName: function (parent, childName) {
                let childNode = parent.getChildByName(childName);
                if (childNode) {
                    return true;
                }
                return false;
            },

            addPrefabUI: function (url, parent, zOrder, callback) {
                let self = this;
                UIHelper.loadRes(url, cc.Prefab, function (prefab) {
                    let prefabUI = self.createPrefab(prefab);
                    parent.addChild(prefabUI, zOrder);
                    if (callback) {
                        callback(prefabUI);
                    }
                });
            },

            showMsgTips: function(content,title,coloseLabel){
                // let parent = cc.director.getScene();
                // if (UIHelper.isHaveChildByName(parent, "msgTips")) {
                //     return;
                // }
                // this.addPrefabUI('Prefab/MsgTips', parent, 100, function (loadingTips) {
                //     loadingTips.name = 'msgTips';
                //     loadingTips.getComponent("MsgTips").setMsgTipsLabel(content,title,coloseLabel);
                // });
            },
        }
    });

    module.exports = UIHelper;